package com.ruoyi.project.channel.vwBusinessCashDayStat.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.vwBusinessCashDayStat.domain.VwBusinessCashDayStat;
import com.ruoyi.project.channel.vwBusinessCashDayStat.service.IVwBusinessCashDayStatService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * VIEW信息操作处理
 * 
 * @author zero
 * @date 2019-05-06
 */
@Controller
@RequestMapping("/channel/vwBusinessCashDayStat")
public class VwBusinessCashDayStatController extends BaseController
{
    private String prefix = "channel/view";
	
	@Autowired
	private IVwBusinessCashDayStatService vwBusinessCashDayStatService;
	
	@RequiresPermissions("channel:vwBusinessCashDayStat:view")
	@GetMapping()
	public String vwBusinessCashDayStat()
	{
	    return prefix + "/vwBusinessCashDayStat";
	}
	
	/**
	 * 查询VIEW列表
	 */
	@RequiresPermissions("channel:vwBusinessCashDayStat:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(VwBusinessCashDayStat vwBusinessCashDayStat)
	{
		startPage();
		// 取身份信息
		User user = getSysUser();
		String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
		if (!roleKey.equals("admin")&& !roleKey.equals("admin2") ) {
			// 非admin 角色 只能查看下级商户信息
			if(roleKey.equals("agent")){
				vwBusinessCashDayStat.setParentUserId(user.getUserId());
			}else if(roleKey.equals("channel")){
				vwBusinessCashDayStat.getParams().put("channelUserId", user.getUserId());
			}else if(roleKey.equals("salesman")){
				vwBusinessCashDayStat.setRootUserId(user.getUserId());
			}else{
				vwBusinessCashDayStat.setUserId(user.getUserId());
			}
		}
        List<VwBusinessCashDayStat> list = vwBusinessCashDayStatService.selectVwBusinessCashDayStatList(vwBusinessCashDayStat);
		return getDataTable(list);
	}

	@RequiresPermissions("channel:vwBusinessCashDayStat:view")
	@GetMapping("/salesman")
	public String vwSalesmanCashDayStat()
	{
		return prefix + "/vwSalesmanCashDayStat";
	}

	/**
	 * 业务员统计数据
	 */
	@RequiresPermissions("channel:vwBusinessCashDayStat:list")
	@PostMapping("/list/salesman")
	@ResponseBody
	public TableDataInfo salesmanList(VwBusinessCashDayStat vwBusinessCashDayStat)
	{
		startPage();
		// 取身份信息
		User user = getSysUser();
		String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
		if (!roleKey.equals("admin")&& !roleKey.equals("admin2") ) {
			vwBusinessCashDayStat.setRootUserId(user.getUserId());
			// 非admin 角色 只能查看下级商户信息
//			if(roleKey.equals("agent")){
//				vwBusinessCashDayStat.setParentUserId(user.getUserId());
//			}else if(roleKey.equals("channel")){
//				vwBusinessCashDayStat.getParams().put("channelUserId", user.getUserId());
//			}else{
//				vwBusinessCashDayStat.setUserId(user.getUserId());
//			}
		}
		List<VwBusinessCashDayStat> list = vwBusinessCashDayStatService.selectVwSalesmanCashDayStatList(vwBusinessCashDayStat);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出VIEW列表
	 */
	@RequiresPermissions("channel:vwBusinessCashDayStat:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VwBusinessCashDayStat vwBusinessCashDayStat)
    {
		// 取身份信息
		User user = getSysUser();
		String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
		if (!roleKey.equals("admin")&& !roleKey.equals("admin2") ) {
			// 非admin 角色 只能查看下级商户信息
			if(roleKey.equals("agent")){
				vwBusinessCashDayStat.setParentUserId(user.getUserId());
			}else{
				vwBusinessCashDayStat.setUserId(user.getUserId());
			}
		}
    	List<VwBusinessCashDayStat> list = vwBusinessCashDayStatService.selectVwBusinessCashDayStatList(vwBusinessCashDayStat);
        ExcelUtil<VwBusinessCashDayStat> util = new ExcelUtil<VwBusinessCashDayStat>(VwBusinessCashDayStat.class);
        return util.exportExcel(list, "vwBusinessCashDayStat");
    }
	
	/**
	 * 新增VIEW
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存VIEW
	 */
	@RequiresPermissions("channel:vwBusinessCashDayStat:add")
	@Log(title = "VIEW", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(VwBusinessCashDayStat vwBusinessCashDayStat)
	{		
		return toAjax(vwBusinessCashDayStatService.insertVwBusinessCashDayStat(vwBusinessCashDayStat));
	}

	/**
	 * 修改VIEW
	 */
	@GetMapping("/edit/{businessId}")
	public String edit(@PathVariable("businessId") Long businessId, ModelMap mmap)
	{
		VwBusinessCashDayStat vwBusinessCashDayStat = vwBusinessCashDayStatService.selectVwBusinessCashDayStatById(businessId);
		mmap.put("vwBusinessCashDayStat", vwBusinessCashDayStat);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存VIEW
	 */
	@RequiresPermissions("channel:vwBusinessCashDayStat:edit")
	@Log(title = "VIEW", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(VwBusinessCashDayStat vwBusinessCashDayStat)
	{		
		return toAjax(vwBusinessCashDayStatService.updateVwBusinessCashDayStat(vwBusinessCashDayStat));
	}
	
	/**
	 * 删除VIEW
	 */
	@RequiresPermissions("channel:vwBusinessCashDayStat:remove")
	@Log(title = "VIEW", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(vwBusinessCashDayStatService.deleteVwBusinessCashDayStatByIds(ids));
	}
	
}
